package controlSoftware.DataIn;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import controlSoftware.userThread.Device;
import controlSoftware.userThread.SensorData;

import de.mud.telnet.*;

public class TelnetParser {
	public TelnetWrapper telnet = new TelnetWrapper();;
	private String host;
	private int port;
	public byte[] buffer = new byte[1024];
	public final char nlChar = '\n';
	public Queue<String> lineOfData = new LinkedList<String>();

	public TelnetParser(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public TelnetWrapper getTelnetConnection() {
		return telnet;
	}

	public boolean connect() {

		// sample code
		try {
			telnet.connect(host, port);
		} catch (IOException e) {
			e.getMessage();
			return false;
		}		
		return true;
	}

	public SensorData getReadings() throws IOException {
		String newStr = "";
		byte[] b = new byte[1024];
		newStr = "";

		telnet.read(b);
		for (int i = 0; i < b.length; i++) {
			if (b[i] != 0) {
				newStr += (char) b[i];
			}
		}
		
		String[]data = newStr.split("\\,");
		Device testObj = new Device (1, "Starfy", "Chang");
		
		SensorData dataObj = new SensorData(
				Double.parseDouble(data[0]),
				Double.parseDouble(data[1]),
				Double.parseDouble(data[2]), 0, 0, 0, testObj);
		
		return dataObj;
	}
}